﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace WZ.WebSpider
{
    public class CompanySpider : IWebSpider<Entity.Commpany>
    {
        public string Code { get; set; }
        string _url = "";
        string _regexStr = "";
        Encoding _contextEncoding = Encoding.UTF8;
        public CompanySpider(string url, string regexStr, Encoding contextEncoding)
        {
            _url = url;
            _regexStr = regexStr;
            _contextEncoding = contextEncoding;
        }

        public string Url
        {
            get { return _url; }
        }
        public Encoding ContextEncoding { get { return _contextEncoding; } }
        public System.Text.RegularExpressions.Regex regex
        {
            get { return new Regex(_regexStr); }
        }
        public List<Entity.Commpany> GetWebData(string urlParam)
        {
            WZ.Utils.WebHttp.HttpClient client = new Utils.WebHttp.HttpClient(this.ContextEncoding);
            string postResult = client.PostString(this.Url, urlParam);
            MatchCollection matchs = regex.Matches(postResult);
            List<Entity.Commpany> companys = new List<Entity.Commpany>();
            for (int i = 0; i < matchs.Count; i++)
            {
                companys.Add(WZ.WebSpider.Serialize.ConverModel.FillModel<Entity.Commpany>(matchs[i].Groups));
            }
            return companys;
        }

        public bool Save(Entity.Commpany t)
        {
            throw new NotImplementedException();
        }

        public int DepIndex
        {
            get
            {
                return 1;
            }
        }
    }
}
